package com.xiaolong.api;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;
import com.xiaolong.base.BaseController;
import com.xiaolong.model.User;
import com.xiaolong.server.IUserService;
import com.xiaolong.server.RemoteService;

@RestController
@RequestMapping(value="/api/test")
public class TestController extends BaseController{
	
	@Resource
	private RemoteService remoteService;
	/**
	 * 采用Ribbon请求数据
	 */
	@Resource
	private RestTemplate client;
	
	@Resource
	private IUserService userService;
	
	@RequestMapping(method = RequestMethod.GET,value="/getname")
	public String getName(){
		return remoteService.getFoo1();
	}
	
	@RequestMapping(method = RequestMethod.GET,value="/getmap")
	public String getMap(){
		Map<String,String> param = Maps.newHashMap();
		param.put("ddd", "test1");
		param.put("ddd1", "test2");
		param.put("ddd2", "test3");
		if(true){
			throw new RuntimeException("ssss");
		}
		return  remoteService.getMap("sss");
		
		//Map<String,String> map = client.postForObject("http://example-app-server/api/demo/map", "ssss", Map.class);
		/*Iterator<String> it = map.keySet().iterator();
		String s = "";
		while(it.hasNext()){
			s +=map.get(it.next());
		}
		return s;*/
	}
	
	@RequestMapping(method = RequestMethod.GET,value="/user")
	public String getUser(){
		
		return remoteService.getUser("dsdddd").getUserName();
	}
	
	@RequestMapping(method = RequestMethod.GET,value="/index")
	public ModelAndView index(User user,Model model){
		User u = userService.get(18);
		model.asMap();
		//System.out.println(u.getUserName());
		//System.out.println(user.getUserName());
		return new ModelAndView("/index");
	}
}
